import Foundation
import Moya

//极光推送相关api
enum AiNetApi {
    case push(jsonString: String) //推送
    case deletePush(jsonString: String) //删除推送
    case aiMinmaxVoice(token:String, group_id :String, jsonString: String) //minmax
    case aiCommon2(url:String, token :String, jsonString: String) //通用的ai
    case aiCommon(url:String, token :String, jsonString: Encodable) //通用的ai
    case aiMinmax(token:String, group_id :String, jsonString: String) //minmax

}

extension AiNetApi: TargetType {
    var baseURL: URL {
        switch self {
        case .push:
//            return URL(string: "http://1.117.181.189:8889/api/msg-notify/save")! //加密版
            return URL(string: "http://1.117.181.189:8889/api/msg-notify-v2/save")! //无加密
        case .deletePush:
//            return URL(string: "http://1.117.181.189:8889/api/msg-notify/delete")! //加密版
            return URL(string: "http://1.117.181.189:8889/api/msg-notify-v2/delete")! //无加密
        case .aiMinmaxVoice:
            return URL(string: "https://api.minimax.chat/v1/t2a_v2")!
        case .aiMinmax:
            return URL(string: "https://api.minimax.chat/v1/text/chatcompletion_pro")!
        case .aiCommon(url: let url, _, _):
            return URL(string: url)!
        case .aiCommon2(url: let url, _, _):
            return URL(string: url)!
        }
    }
    
    var path: String {
        switch self {
        case .push:
            return ""
        case .deletePush:
            return ""
        case .aiMinmaxVoice(_, _, _):
            return ""
        case .aiMinmax(_, _, _):
            return ""
        case .aiCommon(_, _, _):
            return ""
        case .aiCommon2(_, _, _):
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .push:
            return .post
        case .deletePush:
            return .post
        case .aiMinmaxVoice:
            return .post
        case .aiMinmax:
            return .post
        case .aiCommon:
            return .post
        case .aiCommon2:
            return .post
        }
    }
    
    var task: Task {
        switch self {
        case .push(let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                //json 跟Query
                return .requestData(jsonData)
            }
            return .requestPlain
        case .deletePush(let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                //json 跟Query
                return .requestData(jsonData)
            }
            return .requestPlain
        case .aiMinmaxVoice(_, let groupId,let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                let dictQuery = ["GroupId": groupId]
                //json 跟Query
                return .requestCompositeData(bodyData: jsonData, urlParameters: dictQuery)
            }
            return .requestPlain
        case .aiMinmax(_, let groupId,let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                let dictQuery = ["GroupId": groupId]
                //json 跟Query
                return .requestCompositeData(bodyData: jsonData, urlParameters: dictQuery)
            }
            return .requestPlain
        case .aiCommon2(_, _ , jsonString: let jsonString):
            if let jsonData = jsonString.data(using: .utf8) {
                //json 跟Query
                return .requestData(jsonData)
            }
            return .requestPlain
        case .aiCommon(_, _ , let jsonString):
            if let jsonData = toData(jsonString) {
                //json 跟Query
                return .requestData(jsonData)
            }
            return .requestPlain
        }
    }
    var headers: [String: String]? {
        var headers: [String: String] = [:]
        
        switch self {
        case .push:
            headers["Accept"] = "application/json"
            headers["Content-Type"] = "application/json"
            return headers
        case .deletePush:
            headers["Accept"] = "application/json"
            headers["Content-Type"] = "application/json"
            return headers
        case .aiMinmaxVoice(let token, _, _):
            headers["Content-Type"] = "application/json"
            headers["Authorization"] = "Bearer \(token)"
            return headers
        case .aiMinmax(let token, _, _):
            headers["Content-Type"] = "application/json"
            headers["Authorization"] = "Bearer \(token)"
            return headers
        case .aiCommon(_, token: let token, _):
            headers["Content-Type"] = "application/json"
            headers["Authorization"] = "Bearer \(token)"
            return headers
        case .aiCommon2(_, token: let token, _):
            headers["Content-Type"] = "application/json"
            headers["Authorization"] = "Bearer \(token)"
            return headers
        }
    }
    
    private var sampleData: Data? {
        // 通常用于模拟网络请求的响应数据
        return nil
    }
    
    var validate: Bool {
        return true  // 根据需要开启或关闭响应验证
    }
}
